using System;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Text;
namespace StudentManagementApplication.DAO
{
    public class LoginDAO : BaseDAO
    {
        public LoginDAO()
        {
        }

        public Entities.Login CreateAccountFromReader(IDataReader reader)
        {
            Entities.Login login = new Entities.Login();
            login.LoginID = (string)reader["cLoginID"];
            login.Password = (string)reader["vPassword"];
            login.RoleID = (string)reader["cRoleID"];
            login.State = (int)reader["iState"];
            return login;
        }

        public void InsertAccount(Entities.Login login)
        {
            using (SqlConnection connection = GetConnection())
            {
                SqlCommand command = new SqlCommand("prcInsertAccount", connection);
                command.CommandType = CommandType.StoredProcedure;

                command.Parameters.Add(new SqlParameter("@cLoginID", login.LoginID));
                command.Parameters.Add(new SqlParameter("@cRoleID",login.RoleID));
                command.Parameters.Add(new SqlParameter("@iState", login.State));
                connection.Open();
                command.ExecuteNonQuery();
                //if (command.ExecuteNonQuery() <= 0)

            }
        }

        public void UpdateAccount(Entities.Login login)
        {
            using (SqlConnection connection = GetConnection())
            {
                SqlCommand command = new SqlCommand("prcUpdateAccount", connection);
                command.CommandType = CommandType.StoredProcedure;

                command.Parameters.Add(new SqlParameter("@cLoginID", login.LoginID));
                command.Parameters.Add(new SqlParameter("@cRoleID",login.RoleID));
                command.Parameters.Add(new SqlParameter("@iState", login.State));

                connection.Open();
                command.ExecuteNonQuery();
                //if (command.ExecuteNonQuery() <= 0)
                //    throw;
            }
        }

        public void DeleteAccount(string sLoginID)
        {
            using (SqlConnection connection = GetConnection())
            {
                SqlCommand command = new SqlCommand("prcDeleteAccount", connection);
                command.CommandType = CommandType.StoredProcedure;

                command.Parameters.Add(new SqlParameter("@cLoginID", sLoginID));

                connection.Open();
                command.ExecuteNonQuery();
                //if (command.ExecuteNonQuery() <= 0)
                //    throw;
            }
        }

        public void UpdatePasswordOfAccount(Entities.Login login)
        {
            using (SqlConnection connection = GetConnection())
            {
                SqlCommand command = new SqlCommand("prcUpdatePasswordOfAccount", connection);
                command.CommandType = CommandType.StoredProcedure;

                command.Parameters.Add(new SqlParameter("@cLoginID", login.LoginID));
                command.Parameters.Add(new SqlParameter("@vPassword", login.Password));

                connection.Open();
                command.ExecuteNonQuery();
                //if (command.ExecuteNonQuery() <= 0)
                //    throw;
            }
        }

        public List<Entities.Login> GetAllAccounts()
        {
            using (SqlConnection connection = GetConnection())
            {
                SqlCommand command = new SqlCommand("prcGetAllAccounts", connection);
                command.CommandType = CommandType.StoredProcedure;

                connection.Open();
                List<Entities.Login> accounts = new List<Entities.Login>();
                using (IDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection))
                {
                    while (reader.Read())
                    {
                        accounts.Add(CreateAccountFromReader(reader));
                    }
                    //else
                    //    throw;
                }
                return accounts;
            }
        }

        public Entities.Login GetAccountByLoginID(string sLoginID)
        {
            using (SqlConnection connection = GetConnection())
            {
                SqlCommand command = new SqlCommand("prcGetAccountByLoginID", connection);
                command.CommandType = CommandType.StoredProcedure;

                command.Parameters.Add(new SqlParameter("cLoginID", sLoginID));

                connection.Open();
                Entities.Login account = null;
                using (IDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection))
                {
                    if(reader.Read())
                    {
                        account = CreateAccountFromReader(reader);
                    }
                    //else
                    //    throw;
                }
                return account;
            }
        }

        public Entities.Login GetAccountByLoginIDAndPassword(string sLoginID,string sPassword)
        {
            using (SqlConnection connection = GetConnection())
            {
                SqlCommand command = new SqlCommand("prcGetAccountByLoginIDAndPassword", connection);
                command.CommandType = CommandType.StoredProcedure;

                command.Parameters.Add(new SqlParameter("cLoginID", sLoginID));
                command.Parameters.Add(new SqlParameter("vPassword", sPassword));

                connection.Open();
                Entities.Login account = null;
                using (IDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection))
                {
                    if (reader.Read())
                    {
                        account = CreateAccountFromReader(reader);
                    }
                    //else
                    //    throw;
                }
                return account;
            }
        }

    }
}
